import javax.swing.plaf.IconUIResource;
import java.util.Arrays;

public class shuzuwork {
    //实现一个方法 transform, 以数组为参数, 循环将数组中的每个元素 乘以 2 , 并设置到对应的数组元素上. 例如 原数组为 {1, 2, 3}, 修改之后为 {2, 4, 6}
    public static void transform(int[] array){
        for (int i = 0; i < array.length; i++) {
            array[i]*=2;
        }
    }

    public static int[] transform2(int[] array){
        int[] ret = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            ret[i] = array[i]*2;
        }
        return ret;
    }


    public static double avg(int[] array){
        double avg = 0;
        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum+=array[i];
            avg=sum/array.length;
        }
        return avg;
    }
    //奇数位于偶数之前
    public static void adjust(int[] array){
        int left = 0;
        int right = array.length-1;

        while(left<right){
            while (left<right && array[left] % 2 == 1){
                left++;
            }
            while (left<right && array[right] % 2 ==0){
                right--;
            }
            if(left<right){
                int tmp = array[right];
                array[right] = array[left];
                array[left] = tmp;
            }
        }
    }

    //两数之和
    public static int[] add(int[] array,int x){
        int i= 0;
        for ( i = 0; i < array.length;i++) {
            for (int j =i+1; j < array.length-1; j++) {
                if(array[i]+array[j]==x){
                    return new int[]{i,j};
                }
            }
        }
        return  new int[]{-1,-1};
    }


    //只出现一次的数字
    //因为两个相同的数字异或等于0 0异或其他数字等于那个数字本身
    public static int only(int[] array){
        int ret = 0;
        for (int i = 0; i < array.length; i++) {
            ret ^= array[i];
        }
        return ret;
    }

    //多数元素 前提存在多属元素的情况下中间值一定是多数元素
    public static  int many(int[] array){
        Arrays.sort(array);
        return array[array.length/2];
    }

    //存在连续三个奇数的数组
    public static boolean continuous(int[] arr){
        for (int i = 0; i <arr.length ; i++) {
            if(arr[i] % 2!= 0 && arr[i + 1] % 2!= 0 && arr[i + 2] % 2!= 0){
                return  true;
            }
        }
        return false;
    }


    public static boolean continuous1(int[] arr){
        int count = 0;
        for (int i = 0; i <arr.length ; i++) {
            if(arr[i] % 2 ==1){
                count++;
                if(count == 3){
                    return true;
                }
            }else {
                count = 0;
            }
            }
        return false;
        }






    public static void main(String[] args) {
        int[] array={4,1,2,1,2};

//        System.out.println(continuous(array));
//        System.out.println(many(array));
//        transform(array);
//        System.out.println(Arrays.toString(array));
//        System.out.println(avg(array));
//
//        adjust(array);
//        System.out.println(Arrays.toString(array));

//        int[] ret = add(array,9);
//        System.out.println(Arrays.toString(ret));

        System.out.println(only(array));
    }
}
